import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import seaborn as sns
from numpy import zeros
from scipy.linalg import svd

# 读入数据

# market = "hair_dryer"
market = "microwave"
# market = "pacifier"
data = pd.read_excel("../Problem_C_Data/" + market + '.xlsx', market)
# 读入行词库
roww = []
with open("C:/Users/jiarui/Desktop/数学建模/MCM-C/数据/剔除后的数据/数据处理/词库.txt") as file:
    lines = file.readlines()
    for l in lines:
        roww.append(l.strip("\n"))
# 读入列id库
columns = (data.loc[:, 'customer_id']).tolist()
col = []
for i in columns:
    col.append(str(i))
# 建立矩阵
count_matrix = pd.DataFrame(0, index=roww, columns=(col))
# 遍历数据中的每一行评论 对其计数
import re

for idx, row in data.iterrows():
    a1 = row['review_headline']
    b1 = row['review_body']
    fil = re.compile(u'[^0-9a-zA-Z\u4e00-\u9fa5.，,。？“”]+', re.UNICODE)
    if type(a1) == float:
        a1 = 'a'
    if type(b1) == float:
        b1 = 'a'
    a = fil.sub(' ', a1).lower()
    b = fil.sub(' ', b1).lower()
    # a.sub('[^a-zA-Z\s]',' ')
    proa = a.split(' ')
    prob = b.split(' ')
    for word in proa:
        if word in roww:
            count_matrix.loc[word, str(row['customer_id'])] += 1
    for word in prob:
        if word in roww:
            count_matrix.loc[word, str(row['customer_id'])] += 1

# 稀疏矩阵的奇异值分解
# M = np.array(count_matrix)
# U,S,V = np.linalg.svd(count_matrix)
# 计算前k个奇异值
from scipy import sparse

MM = M.astype(np.float64)
U, S, V = sparse.linalg.svds(MM, k=3)
U, S, V = U[:, ::-1], S[::-1], V[::-1, :]

# 取前两个维度进行可视化展示
view = (pd.DataFrame(V, columns=columns)).iloc[[0, 1], :].T
view.columns = ['x1', 'x2']
# 剔除异常值
view1 = view[(np.abs(view.iloc[:, 1] + view.iloc[:, 0]) <= 0.1)]

# 画图展示
# g = sns.jointplot(x="x1", y="x2", data=view1, kind="kde", color="m")
# g.plot_joint(plt.scatter, c="w", s=30, linewidth=1, marker="+")
# g.ax_joint.collections[0].set_alpha(0)
# g.set_axis_labels("$X$", "$Y$");

sns.jointplot(x="x1", y="x2", data=view1);
with sns.axes_style("white"):
    # ax.xlim(-0.15, 0.1)
    # sns.ylim(-0.15, 0.1)
    sns.jointplot(x=view1.loc[:, 'x1'], y=view1.loc[:, 'x2'], kind="hex", color="k");

# 核密度的计算和展示
import numpy as np
import matplotlib.pyplot as pl
import scipy.stats as st

x = view.iloc[:, 0]
y = view.iloc[:, 1]
xmin, xmax = -0.02, 0.02
ymin, ymax = -0.02, 0.02

# Peform the kernel density estimate
xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([xx.ravel(), yy.ravel()])
values = np.vstack([x, y])
kernel = st.gaussian_kde(values)
f = np.reshape(kernel(positions).T, xx.shape)

fig = pl.figure()
ax = fig.gca()
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
# Contourf plot
cfset = ax.contourf(xx, yy, f, cmap='RdBu_r')
## Or kernel density estimate plot instead of the contourf plot
# ax.imshow(np.rot90(f), cmap='Blues', extent=[xmin, xmax, ymin, ymax])
# Contour plot
cset = ax.contour(xx, yy, f, colors='k')
# Label plot
# ax.clabel(cset, inline=1, fontsize=10)
ax.set_xlabel('Y1')
ax.set_ylabel('Y0')

pl.show()

# 三维图像
from mpl_toolkits.mplot3d import Axes3D

fig = pl.figure()  # 定义新的三维坐标轴
ax3 = pl.axes(projection='3d')

# 定义三维数据
x = np.linspace(xmin, xmax, 100)
y = np.linspace(ymin, ymax, 100)
xx = np.arange(xmin, xmax, 100)
yy = np.arange(ymin, ymax, 100)
X, Y = np.meshgrid(x, y)
Z = f

# 作图
pl.title('KDE for Eigenvector Aggregation')
ax3.plot_surface(X, Y, Z, cmap='RdBu_r')
# ax3.contour(X,Y,Z, zdim='z',offset=-2，cmap='rainbow)   #等高线图，要设置offset，为Z的最小值
pl.show()
# ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap='rainbow')
